<?php

class user
{
	public $db;
	
	public $id;
	public $email;
	public $password;
	public $name;
	public $surname;
	public $address;
	public $phone;
	public $city_id;
	public $city;
	public $emailConfirm;
	public $status;	
	public $level = 0;
	public $activation_id;
	
	public $bonus;
	
	function __construct() 
	{
		$this->db = new database();
	}
	
	function login($email, $password)
	{
		$result = $this->db->select("SELECT `u`.`ID`, `u`.`surname`, `u`.`name`, `u`.`address`, `u`.`emailConfirm`, `u`.`phone`, `u`.`status`, `u`.`level`, `c`.`ID` AS 'cityid', `c`.`name` AS 'cityname' FROM `user` AS `u`, `city` AS `c` WHERE `u`.`email` = '". $email ."' AND `u`.`password` = '". $password ."' AND `u`.`city_ID` = `c`.`ID`");
		
		if(mysql_num_rows($result) != 1) {
			
			return false;
		} else {
			$row = mysql_fetch_array($result);
			
			$this->id = $row['ID'];
			$this->email = $email;
			$this->password = $password;
			$this->surname = $row['surname'];
			$this->name = $row['name'];
			$this->address = $row['address'];
			$this->city_id = $row['cityid'];
			$this->city = $row['cityname'];
			$this->emailConfirm = $row['emailConfirm'];
			$this->phone = $row['phone'];
			$this->status = $row['status'];
			$this->level = $row['level'];
			
			$this->setBonus();
			
			$this->setSession();
			
			return true;	
		}
	}
	
	function fromArray($row)
	{		
		$this->id = $row['ID'];
		$this->email = $row['email'];
		$this->password = $row['password'];
		$this->surname = $row['surname'];
		$this->name = $row['name'];
		$this->address = $row['address'];
		$this->city_id = $row['cityid'];
		$this->city = $row['cityname'];
		$this->emailConfirm = $row['emailConfirm'];
		$this->phone = $row['phone'];
		$this->status = $row['status'];
		$this->level = $row['level'];
	}
	
	function logout()
	{
		if($this->isLogged()) {
			session_destroy();
			
			return true;
		} else {
			return false;
		}
	}
	
	function setSession() 
	{
		$_SESSION['user'] = serialize($this);
	}
	
	function isLogged() 
	{
		if(isset($_SESSION['user']) || isset($_COOKIE['login_email'])) {
			return true;
		} else {
			return false;
		}
	}
	
	function setBonus()
	{
		$this->bonus = 0;
		
		$bonus_q = $this->db->select("SELECT * FROM `bonus` WHERE `user_ID` = '" . $this->id . "' AND `status` = 1");
			
		while($bonus = mysql_fetch_array($bonus_q)) {
			$this->bonus = $this->bonus + $bonus['value'];
		}
	}
	
	function getBonus()
	{	
		$bonus = $this->bonus;
	
		if(strpos($bonus, ".")) {
			while(strlen(substr($bonus,strpos($bonus,".")+1)) < 2) {
				$bonus = $bonus.'0';
			}
		} else {
			$bonus = $bonus.'.00';
		}
		
		if(strlen(substr($bonus,0,strpos($bonus,"."))) == 0) {
			$bonus = '0'.$bonus;
		}

		return $bonus;
	}
	
	function validateField($field, $value)
	{
		switch ($field) {
			case "name":
				if(strlen($value)<3 OR strlen($value)>40) {
					return false;
				} else {
					return true;
				}
			break;
			case "surname":
				if(strlen($value)<3 OR strlen($value)>40) {
					return false;
				} else {
					return true;
				}
				break;
			case "email":
				if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $value) OR $value == "") {
					return false;
				} else {
					return true;
				}
			break;
			case "emailFree":
				if(mysql_num_rows($this->db->select("SELECT * FROM `user` WHERE `email` = '".$value."'")) > 0) {
					return false;
				} else {
					return true;
				}
			break;
			case "password":
				if($_POST['password'] == "") {
					return false;
				} else {
					return true;
				}
			break;
			case "address":
				if($value == "") {
					return false;
				} else {
					return true;
				}
			break;
			case "city":
				if(!$this->db->exist("SELECT * FROM `city` WHERE `id`='".$value."'")) {
					return false;
				} else {
					return true;
				}
			break;
			case "phone":
				if(strlen($value)>0) {
					if(strlen($value)!=10 || !is_numeric($value)) {
						return false;
					} else {
						return true;
					}
				} else {
					return false;
				}
			break;
		}
	}
	
	function toDB($id = false)
	{	
		if(!$id) {
			$insert_id = $this->db->insert("INSERT INTO `user` VALUES(null, '".$this->city_id."', '".$this->name."', '".$this->surname."', '".$this->address."', '".$this->email."', '".$this->emailConfirm."', '".$this->password."', '".$this->phone."', '".$this->status."', '".$this->level."', '".$this->activation_id."')");
			
			if(!$insert_id) {
				return false;
			} else {
				return $insert_id;
			}
		} else {
			if($this->db->update("UPDATE user SET `name`='".$this->name."', `surname`='".$this->surname."', `address`='".$this->address."', `city_ID`='".$this->city_id."', `emailConfirm`='".$this->emailConfirm."', `phone`='".$this->phone."', `password`='".$this->password."', `status`='".$this->status."', `level`='".$this->level."' WHERE `id`='".$id."'")) {
				$this->setSession();
				
				return true;
			} else {
				return false;
			}
		}
	}
	
	// Get
	
	function getUsername()
	{
		return $this->name . ' ' . $this->surname;
	}
	
	function activate($email, $activation_id)
	{
		$result = $this->db->select("SELECT * FROM `user` WHERE `email` = '".$email."' AND `activation_id` = '" . $activation_id . "' AND `emailConfirm` = 0");
		if(mysql_num_rows($result) > 0) {
			if($this->db->update("UPDATE user SET `emailConfirm` = 1 WHERE `email` = '".$email."' LIMIT 1")) {
				$row = mysql_fetch_array($result);
				$this->db->insert("INSERT INTO `bonus` VALUES(NULL, " . $row['ID'] . ", 5, 'Bonus per conferma email', 1)");
				return $row['password'];
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	function getStatus()
	{
		if($this->status == -1) {
			
			return 'non confermato';
		} else if($this->status == 0) {
			
			return 'confermato';
		} else {
			
			$return = 'dipendente';
			$return .= ' [ ';
				switch($this->level) {
					case 1:
						$return .= 'ordinazioni';
						break;
					case 2:
						$return .= 'pizzeria';
						break;
					case 3:
						$return .= 'consegna';
						break;
					case 4:
						$return .= 'admin';
						break;
				}
			$return .= ' ] ';
			
			return $return;
		}
	}
	
}

?>